<?php

namespace App\Exceptions;

use Exception;

class ApiException extends Exception
{
    //laravel 自定义 ApiException 继承 Exception
    public const SYSTEM_ERROR_CODE = 10000;
    public const SYSTEM_ERROR_CODE_MSG = '变量不存在';

    private static $error_config = [
        self::SYSTEM_ERROR_CODE => self::SYSTEM_ERROR_CODE_MSG,
    ];

    public function __construct($code = self::SYSTEM_ERROR_CODE, $message = self::SYSTEM_ERROR_CODE_MSG)
    {
        if (isset(static::$error_config[$code])) {
            $message = static::$error_config[$code];
        }
        parent::__construct($message, $code);
    }

    public function render()
    {
        return response()->json([
            'code' => $this->getCode(),
            'message' => $this->getMessage(),
        ]);
    }

}
